package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.consumptionRecord;
import com.trxy.hotelsystem.model.room;
import com.trxy.hotelsystem.model.supermarket;
import com.trxy.hotelsystem.service.impl.Consumption_recordServiceImpl;
import com.trxy.hotelsystem.service.impl.SupermarketServiceImpl;
import com.trxy.hotelsystem.service.impl.VipServiceImpl;

import frame.uitls.PrimaryKeyUtil;
import net.sf.json.JSONArray;

@Controller
public class SupermarketContriller {
	
	@Autowired
	private SupermarketServiceImpl supermarketServiceImpl;
	
	@Autowired
	private VipServiceImpl VipServiceimpl;
	
	@Autowired
	private Consumption_recordServiceImpl Consumption_recordServiceimpl;
	
	
	//查询超市所有商品
	@RequestMapping(value = "querySupermarketAll.do")
	public String querySupermarketAll(HttpServletRequest request,HttpServletResponse response,
			Model model,supermarket supermarket,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<supermarket> list = supermarketServiceImpl.querySupermarketAll();
		
		 PageInfo<supermarket> page = new PageInfo<>(list);

		 model.addAttribute("pageInfo", page);
		
		return "view/supermarket/stafsupermarket";
	}
	
	//查询超市所有商品
	@RequestMapping(value = "querySupermarketAll2.do")
	public String querySupermarketAll2(HttpServletRequest request,HttpServletResponse response,
			Model model,supermarket supermarket,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<supermarket> list = supermarketServiceImpl.querySupermarketAll();
		
		 PageInfo<supermarket> page = new PageInfo<>(list);

		 model.addAttribute("pageInfo", page);
		
		return "view/supermarket/supermarketMain";
	}
	
	//购买商品
	@RequestMapping(value = "/buyCommodity.do")
	public void buyCommodity(HttpServletRequest request,HttpServletResponse response,
			Model model,supermarket supermarket,HttpSession session,
			clientInf clientinf,consumptionRecord consumptionrecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String roomNumber = request.getParameter("roomNumber");
		
		PrintWriter pw = response.getWriter();
		
		//获取前端参数
		String id = request.getParameter("id");
		int amount =  Integer.parseInt(request.getParameter("amount"));
		
		
		supermarket.setReserved1(id);
		
		//查询商品现有数量
		supermarket superm = supermarketServiceImpl.selectSupermarketById(supermarket);
		
		if(superm.getQuantity() < amount) { //数量不够
			pw.write("0");
			
		}else {
			 
			//减少超市商品数量
			 int i = superm.getQuantity() - amount;
			superm.setQuantity(i);
			int j = supermarketServiceImpl.updateSupermarketByquantity(superm);
			
			//获取session值
			clientInf us = (clientInf) session.getAttribute("viplogin");
			
			//减少客户余额
			double money = us.getMoney() - superm.getPrice();
			clientinf.setNumber(us.getNumber());
			clientinf.setMoney(money);
			int k = VipServiceimpl.updeVipselect(clientinf);
			
			consumptionrecord.setSerialnum(PrimaryKeyUtil.getPrimaryKey());
			consumptionrecord.setRoomnumber(Integer.parseInt(roomNumber));
			consumptionrecord.setProduct(superm.getProduct());
			consumptionrecord.setClassify(superm.getClassify());
			consumptionrecord.setPrice(superm.getPrice());
			consumptionrecord.setQuantity(amount);
			consumptionrecord.setTime(new Date());
			consumptionrecord.setState("0");
			consumptionrecord.setReserved1(us.getNumber());
			//创建消费记录
			int m = Consumption_recordServiceimpl.insertConsumption_record(consumptionrecord);
			
			if(j > 0 && k > 0 && m > 0) {
				pw.write("1"); //购买成功
			}else {
				pw.write("0");  //购买失败
			}
			
		}
		
	}
	
	//通过商品ID查询超市商品
	@RequestMapping(value = "selectSupermarketById.do")
	public void selectSupermarketById(HttpServletRequest request,HttpServletResponse response,
			Model model,supermarket supermarket,HttpSession session,room room) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id = request.getParameter("id");
		supermarket.setReserved1(id);
		
		//查询超市数据库
		supermarket sup = supermarketServiceImpl.selectSupermarketById(supermarket);
		
		//JSON格式返回数据
		String st = JSONArray.fromObject(sup).toString();
		response.getWriter().write(st);	
		
	}
	
	//通过商品名、商品分类查询超市--会员
	@RequestMapping(value = "/selsectSupermarket.do")
	public String selsectSupermarket(HttpServletRequest request,HttpServletResponse response,
			Model model,supermarket supermarket,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String product = request.getParameter("product");
		String classify = request.getParameter("classify");
		
		supermarket.setProduct(product);
		supermarket.setClassify(classify);
		
		List<supermarket> list = supermarketServiceImpl.selsectSupermarket(supermarket);
		
		PageInfo<supermarket> page = new PageInfo<>(list);

		model.addAttribute("pageInfo", page);
		model.addAttribute("product", product);
		model.addAttribute("classify", classify);
		model.addAttribute("inf", "多条件");
		
		return "view/supermarket/supermarketMain";
	}
	
	//通过商品名、商品分类查询超市--员工
		@RequestMapping(value = "/selsectSupermarket2.do")
		public String selsectSupermarket2(HttpServletRequest request,HttpServletResponse response,
				Model model,supermarket supermarket,
				@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			//开启分页查询
			PageHelper.startPage(pageNum, pageSize);
			
			String product = request.getParameter("product");
			String classify = request.getParameter("classify");
			
			supermarket.setProduct(product);
			supermarket.setClassify(classify);
			
			List<supermarket> list = supermarketServiceImpl.selsectSupermarket(supermarket);
			
			PageInfo<supermarket> page = new PageInfo<>(list);

			model.addAttribute("pageInfo", page);
			model.addAttribute("product", product);
			model.addAttribute("classify", classify);
			model.addAttribute("inf", "多条件");
			
			return "view/supermarket/stafsupermarket";
		}
	
	//商品下架
	@RequestMapping(value = "soldOut.do")
	public void soldOut(HttpServletRequest request,HttpServletResponse response,
			supermarket supermarket) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String id = request.getParameter("id");
		
		int i = supermarketServiceImpl.deleteSupermarketById(id);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
	}
	
	//添加超市商品
	@RequestMapping(value = "/addsupermaket.do")
	public void addsupermaket(HttpServletRequest request,HttpServletResponse response,
			supermarket supermarket) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		PrintWriter pw = response.getWriter();

		String product = request.getParameter("product");
		String classify = request.getParameter("classify");
		String quantity = request.getParameter("quantity");
		String remarks = request.getParameter("remarks");
		String price = request.getParameter("price");
		
		supermarket.setProduct(product);
		supermarket.setClassify(classify);
		supermarket.setQuantity(Integer.parseInt(quantity));
		supermarket.setRemark(remarks);
		supermarket.setPrice(Double.parseDouble(price));
		supermarket.setReserved1(PrimaryKeyUtil.getPrimaryKey());
		
		int i = supermarketServiceImpl.insertSupermarket(supermarket);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
	}
	
	//添加超市商品数量
	@RequestMapping(value = "addSuperquantity.do")
	public void addSuperquantity(HttpServletRequest request,HttpServletResponse response,
			supermarket supermarket) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		PrintWriter pw = response.getWriter();
		
		String supermarketId = request.getParameter("supermarketId");
		String addquantity = request.getParameter("addquantity");
		String quantity = request.getParameter("quantity");
		int sum = Integer.parseInt(quantity) + Integer.parseInt(addquantity);
		
		
		supermarket.setReserved1(supermarketId);
		supermarket.setQuantity(sum);
		
		int i = supermarketServiceImpl.updateSupermarketByquantity(supermarket);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
	}
	

}
